/**
 * service_favorite js file;
 * company:junqitecch
 * author:xuzuiotao
 * Table: users_favorite
Columns:
id	int(11) AI PK
collection_type	int(2)
user_id	int(11)
content_id	int(11)
create_time	varchar(64)
 * 
 * 
 */



var  consts  = require('../consts');
var  favorite = require('../db/favorite');
var cache=require('../redis/usercache');

//var contacts = require('../db/contact');




function processList(request,response,callback){
	console.log('FavoriteList');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		favorite.getFavoriteList(entity.userID,function(err, list){
			if(err!==null){

				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				var ob={};
				ob.count=list.length;
				ob.items=list;
				response.data = JSON.stringify(ob);
				response.token=tk;
				console.log('mmmmmmm');
				if(callback){
					callback(response);
				}
				
			}
			
		});

	
		
	});

}

function processDelete(request,response,callback){
	console.log('favorite delete');
	

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}


	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		favorite.remove(entity.userID,param.ID,function(err){
			if(err!==null){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.token=tk;
				if(callback){
					callback(response);
	
				}
				
			}
			
		});

	
		
	});
}

module.exports = {

		processList:processList,
		processDelete:processDelete,

};

